<template>
  <div class='goods_container'>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <!-- 搜索与添加区域 -->
      <el-row :gutter="20">
        <el-col :span="7">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getGoodsList">
            <el-button slot="append" icon="el-icon-search" @click="queryGoods"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addGoods">添加商品</el-button>
        </el-col>
      </el-row>

      <el-table :data="goodsList" border stripe>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column prop="goods_price" label="价格(元)" width="100px"></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="100px"></el-table-column>
        <el-table-column label="创建时间" width="200px">
          <template scope="{ row }">
            <span>{{ row.add_time | dateFormat }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200px">
          <template>
            <el-button class="el-icon-edit" size="mini" type="primary"></el-button>
            <el-button class="el-icon-delete" size="mini" type="danger"></el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]" :page-size="queryInfo.pagesize" layout="total, sizes, prev, pager, next, jumper"
        :total="total" background>
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import goods from '@/api/goods'

export default {
  name: 'MyGoods',
  data () {
    return {
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      goodsList: [],
      total: 0
    }
  },
  components: {
  },
  methods: {
    // 1.获取商品列表
    async getGoodsList () {
      const { data: res } = await goods.getGoodsList(this.queryInfo)
      if (res.meta.status === 200) {
        this.goodsList = res.data.goods
        this.total = res.data.total
      } else {
        this.$message.error(res.meta.msg)
      }
    },

    // 2.更改分页大小
    handleSizeChange (val) {
      this.queryInfo.pagesize = val
      this.getGoodsList()
    },

    // 3.更改当前页
    handleCurrentChange (val) {
      this.queryInfo.pagenum = val
      this.getGoodsList()
    },

    // 4.条件查询商品
    queryGoods () {
      this.getGoodsList()
    },

    // 5.添加商品
    addGoods () {
      this.$router.push('/goods/add')
    }
  },
  created () {
    this.getGoodsList()
  },
  mounted () {
  }
}
</script>

<style lang='less' scoped>
.el-breadcrumb {
  line-height: 50px;
}

.el-table {
  margin-top: 15px;
}

.el-pagination {
  margin-top: 15px;
}
</style>
